
#include "base/StructType.hpp"
#include "base/Macro.hpp"
#include "common/MemCommon.hpp"
#include "config/global_config.hpp"
#include "common/CellCommon.hpp"
#include "app/app.hpp"
#include "common/FileCommon.hpp"
#include "util/Date.hpp"
#include "sql/Parse.hpp"
#include "util/Log.hpp"
#include "server/connect.hpp"


void testCreateTable(){
    std::vector<FieldInfo> files;
    files.push_back(FieldInfo{"sqlId",FieldType::INT});
    files.push_back(FieldInfo{"next",FieldType::INT});
    files.push_back(FieldInfo{"prev",FieldType::INT});
    files.push_back(FieldInfo{"transactionId",FieldType::INT});
    files.push_back(FieldInfo{"rollbackId",FieldType::INT});
    files.push_back(FieldInfo{"id",FieldType::INT});
    files.push_back(FieldInfo{"name",FieldType::STRING});
    files.push_back(FieldInfo{"address",FieldType::STRING});
    files.push_back(FieldInfo{"date_rq",FieldType::DATETIME});
    std::string name = "pixel";
//    createTable(name,files);
}
void testReadTable(){

    std::fstream inFile("d:\\pain2.ibd", std::ios::in | std::ios::out | std::ios::binary);
    if (!inFile) {
        std::cerr << "无法打开文件2" << std::endl;
    }
    FileHeader fileHeader{};
    FileCommon::readTableFileHeader(inFile,fileHeader);
    std::cout << "lastPage: " << fileHeader.lastPage<< std::endl;
    std::cout << "pageCount: " << fileHeader.pageCount<< std::endl;
    std::cout << "schemaCount: " << fileHeader.schemaCount<< std::endl;


    std::vector<FieldInfo> fieldInfo(fileHeader.schemaCount);
    FileCommon::readTableFieldList(inFile,fieldInfo);

//    for (FieldInfo fieldInfo1: fieldInfo) {
//        std::cout << "type: " << (int)fieldInfo1.type<< std::endl;
//        std::cout << "name: " << fieldInfo1.name<< std::endl;
//    }
//    std::vector<FieldInfo> fieldInfo{};
//    fieldInfo.resize(fileHeader.schemaCount);
//    readTableFieldList(inFile,fileHeader,fieldInfo);
//    for (FieldInfo f: fieldInfo) {
//        std::cout << "f: " << f.name << std::endl;
//    }
}

void testInsertData(){
//    std::ifstream inFile("d:\\pain2.ibd", std::ios::binary);
    std::fstream inFile("d:\\pain2.ibd", std::ios::in | std::ios::out | std::ios::binary);
    if (!inFile) {
        std::cerr << "无法打开文件" << std::endl;
    }
    FileHeader fileHeader;
    FileCommon::readTableFileHeader(inFile,fileHeader);
    std::cout << "lastPage: " << fileHeader.lastPage<< std::endl;
    std::cout << "pageCount: " << fileHeader.pageCount<< std::endl;
    std::cout << "schemaCount: " << fileHeader.schemaCount<< std::endl;

    std::vector<FieldInfo> fieldInfo{};
    fieldInfo.resize(fileHeader.schemaCount);
    FileCommon::readTableFieldList(inFile,fieldInfo);
    for (FieldInfo f: fieldInfo) {
        std::cout << "f: " << f.name << std::endl;
    }
    std::vector<Cell> cells;
    const char * name =  "张三";
    const char * addr =  "nanking";
//    uint8_t date[5];

    cells.push_back(Cell{false,FieldType::INT,fileHeader.maxRecordNo});
    cells.push_back(Cell{false,FieldType::INT,0});
    cells.push_back(Cell{false,FieldType::INT,0});
    cells.push_back(Cell{false,FieldType::INT,0});
    cells.push_back(Cell{false,FieldType::INT,0});
    cells.push_back(Cell{false,FieldType::INT,3});
    cells.push_back(Cell{false,FieldType::STRING,0,name});
    cells.push_back(Cell{false,FieldType::STRING,0,addr});
    Cell dt{false,FieldType::DATETIME,0,"",""};
    parseDatetimeStringTo5("2025-08-11 12:30:01",dt.datetime);
//    memcpy(dt.datetime, date, 5);
    cells.push_back(dt);


    std::vector<uint8_t> row(PAGE_SIZE);

    PageHead pageHead{};
    pageHead.pageNo = fileHeader.lastPage;
    pageHead.pageNext = 0;
    pageHead.pagePrev = 0;
    pageHead.recordCount = 0;
    pageHead.maxTrxId = 0;
    pageHead.firstRecord = PAGE_HEAD_SIZE;
    memcpy(row.data(),&pageHead,PAGE_HEAD_SIZE);


//    CellCommon::cellToVector(row,cells,row.data() + PAGE_HEAD_SIZE);
    int ret = FileCommon::writePage(inFile,fileHeader.lastPage,row);
    if(ret != 0){
        std::cerr << "writePage create record error " << std::endl;
    }
}
void testReadData(){
    std::fstream inFile("d:\\pain2.ibd", std::ios::in | std::ios::out | std::ios::binary);
    if (!inFile) {
        std::cerr << "无法打开文件" << std::endl;
    }

    std::vector<uint8_t> row(PAGE_SIZE);

    int ret =FileCommon::readPage(inFile,3,row);
    if (ret != 0) {
        std::cerr << "readPage error " << std::endl;
    }
    PageHead pageHead{};
    ret = MemCommon::copyPageHeader(row,pageHead,PAGE_HEAD_SIZE);
    if(ret != 0){
        std::cerr << "copyPageHeader error " << std::endl;
    }
    std::cout << "pageNo: " << pageHead.pageNo<< std::endl;
    std::cout << "firstRecord: " << pageHead.firstRecord<< std::endl;
    std::vector<FieldInfo> fieldList(8);
    FileCommon::readTableFieldList(inFile,fieldList);


    std::vector<Cell> cells;
    CellCommon::vectorToCell(cells,row,fieldList,row.data() + PAGE_HEAD_SIZE);
    nlohmann::json j{};
    to_json(j,cells,fieldList);
    std::cout << "cell toJson: " << j.dump(4) << std::endl;


}

void testInsertRecord(){
    std::vector<Cell> cells;
    const char * name =  "张三223hhhh2";
    const char * addr =  "ninikagnkan2g";
    cells.push_back(Cell{false,FieldType::INT,0});
    cells.push_back(Cell{false,FieldType::INT,0});
    cells.push_back(Cell{false,FieldType::INT,0});
    cells.push_back(Cell{false,FieldType::INT,0});
    cells.push_back(Cell{false,FieldType::INT,0});
    cells.push_back(Cell{false,FieldType::INT,3});
    cells.push_back(Cell{false,FieldType::STRING,0,name});
    cells.push_back(Cell{false,FieldType::STRING,0,addr});
    Cell dt{false,FieldType::DATETIME,0,"",""};
    parseDatetimeStringTo5("2025-08-11 13:30:01",dt.datetime);
    cells.push_back(dt);
//    insertRecord("pixel",cells);
}
void testReadRecord(){
//    readRecordList("pixel", nullptr);
}
void testSqlParse(){
//    const std::string sql = "create table xx(aa varchar(200),bb int,cc datetime)";
//    std::vector<FieldInfo> files;
//    std::string tableName = "pixel";
//    parseCreateTableSql(sql,tableName,files);
//    for (FieldInfo f:files) {
//        std::cout << "name: " << f.name<< std::endl;
//        std::cout << "name: " << (int)f.type<< std::endl;
//    }
    std::fstream file("D:\\pain2.ibd", std::ios::in | std::ios::out | std::ios::binary);
    if (!file) {
        std::cerr << "无法打开文件 insertRecord D:\\pain2.ibd"  << std::endl;
    }
    const std::string sql = "insert into pixel(name,id)values('str',12)";
//    std::vector<FieldInfo> schema;
    std::vector<FieldInfo> fieldList(9);
    FileCommon::readTableFieldList(file,fieldList);
    std::cout << "fieldList: " << fieldList.at(1).name<< std::endl;
    std::string table;
    std::vector<Cell> cells;
    if (buildCellsFromInsert(sql, fieldList, table, cells)) {
        // 这里的 cells 已按 schema 对齐：INT 填 i64，STRING 填 str，DATETIME 填 datetime[5]
//        insertRecord(table, cells); // 你的已有入口
        std::cout << "name: " << table<< std::endl;
        Log::log(cells,fieldList);

    }else{
        std::cout << "error: " << table<< std::endl;
    }
}




//
//#include "server/connect.hpp"
//#include "config/global_config.hpp"
//#include "core/file/file_util.hpp"
//#include "core/file/write_file.hpp"
//#include "core/sql/sql.hpp"
//
//void testCreateTable(){
//    std::vector<FieldInfo> files;
//    files.push_back(FieldInfo{"sqlId",FieldType::INT32,20});
//    files.push_back(FieldInfo{"transactionId",FieldType::INT32,20});
//    files.push_back(FieldInfo{"rollbackId",FieldType::INT32,20});
//    files.push_back(FieldInfo{"id",FieldType::INT32,20});
//    files.push_back(FieldInfo{"name",FieldType::VARCHAR,30});
//    files.push_back(FieldInfo{"address",FieldType::VARCHAR,40});
//    createTable("pixel",files);
//}
//
//void testReadTable(){
//    std::vector<FieldInfo> asd{};
//    FileHeader fileHeader;
//    readTableFields("pixel",asd,fileHeader);
//    painLog(fileHeader);
//    painLog(asd);
//}
//void testInsert(){
//    FileHeader fileHeader;
//    std::vector<FieldInfo> fieldInfos;
//    readTableFields("pixel",fieldInfos,fileHeader);
//
//    DataConfig dc;
//    findDataConfig("pixel",dc);
//
//    std::fstream  outFile(dc.tablePath, std::ios::in | std::ios::out | std::ios::binary);
//    if (!outFile) {
//        std::cerr << "无法创建文件" << std::endl;
//    }
//
//    std::vector<Cell> values;
//    const char* name = "张三";
//    values.push_back(Cell{false,1});
//    values.push_back(Cell{false,0});
//    values.push_back(Cell{false,0});
//    values.push_back(Cell{false,333});
//    values.push_back(Cell{false, 0,name,static_cast<uint16_t>(strlen(name))});
//    values.push_back(Cell{false, 0,"aiy",static_cast<uint16_t>(strlen("aiy"))});
//}
//int main() {
//    painInit();
////    testCreateTable();
////    testReadTable();
////    testInsertPage();
////    testReadPage();
//
//    testInsert();
//
//
//
//
////    testWriteIbd();
////    testRaadIbd();
////    FileUtil::writeFile(g_idbPath, "aabb");
//
////    FileUtil::writeFile("d:/test.bin", 1234);
//    return 0;
//// #ifdef _WIN32
////     // 强制设置控制台编码为UTF-8
////     SetConsoleOutputCP(CP_UTF8);
////     SetConsoleCP(CP_UTF8);
//
////     // 设置控制台字体为支持中文的字体
////     CONSOLE_FONT_INFOEX cfi;
////     cfi.cbSize = sizeof(cfi);
////     cfi.nFont = 0;
////     cfi.dwFontSize.X = 0;
////     cfi.dwFontSize.Y = 16;
////     cfi.FontFamily = FF_DONTCARE;
////     cfi.FontWeight = FW_NORMAL;
////     wcscpy_s(cfi.FaceName, L"Consolas");
////     SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);
//// #endif
//
////     std::cout << "启动MySQL模拟服务器..." << std::endl;
//
////     MySQLServer server(MYSQL_PORT);
//
////     // 设置信号处理 (可选)
////     std::cout << "按 Ctrl+C 停止服务器" << std::endl;
//
////     try {
////         server.start();
////     } catch (const std::exception& e) {
////         std::cerr << "服务器异常: " << e.what() << std::endl;
////         return 1;
////     }
//
////     return 0;
//}
///* 关于文件存储。ibd文件 head,
// *
// *
// * */
/*
 * 代办，binlog,undolog.redolog  内存池，sql多条件，锁。事务。分布式
 *
 * */
int main() {
    painInit();
//    index("create table pixel(id int primary key,aa varchar(200),bb int,cc datetime)");
//    index("insert into pixel(id,aa,bb,cc)values(2,'stra',122,'2025-08-11 12:30:01')");
//    index("insert into pixel(id,aa,bb,cc)values(1,'stra',122,'2025-08-11 12:30:01')");
//    index("insert into pixel(id,aa,bb,cc)values(3,'stra',122,'2025-08-11 12:30:01')");
////    std::string sql3 = "select * from pixel";
//    std::string sql3 = "select * from pixel where id = 3";
//    index(sql3);
// select * from pixel  update pixel set aa='asdjjk1' where id = 1;


/*


 create table pixel(id int primary key,aa varchar(200),bb int,cc datetime)
 insert into pixel(id,aa,bb,cc)values(2,'stra',122,'2025-08-11 12:30:01')
 insert into pixel(id,aa,bb,cc)values(1,'stra',122,'2025-08-11 12:30:01')
 insert into pixel(id,aa,bb,cc)values(3,'stra',122,'2025-08-11 12:30:01')


 update pixel set aa = 'xxxxx' where id = 2;
 */


//    testCreateTable();
//    testInsertRecord();
//    testReadRecord();
//    testReadTable();
//    testInsertData();
//    testReadData();

//    testSqlParse();
    return 0;
}